// manager/pages/customerfiling/index.js
import config from '../../../utils/config';
import { getSystemInfo } from '../../../utils/getSystemInfo';
import { request } from '../../../utils/request';
import util from '../../../common/util';
import WxValidate from '../../../common/WxValidate';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseUrl: config.baseUrl,
        userInfo: {},
        isCalculation: true,

        // 是否允许删除
        isDelete: "",
        // 是否开启删除流程
        isDeleteProcess: "",
        
        subActiveValue: "",
        subTab: [],

        showFilter: false,
        params: {
            search: "",
            phone: "",
            perType: "", // 个人客户类型
            areaName: "", // 网格名称       
            industry: "",
            locationName: "",
            agrType: "", // 农业主体类型
            agrLicenseName: "", // 营业执照名称
        },
        filterTypeIndex: 0,
        personFilterTypes: [
            { id: 1, text: '展示个人列表' },
            { id: 2, text: '展示网格列表' },
        ],
        filterTypes: [
            { id: 1, text: '展示企业列表' },
            { id: 2, text: '展示楼宇列表' },
        ],
        isFilter: false,
        // 客户字典
        typeList: [],
        typeIndex: -1,
        // 个人类型
        personTypeList: [],
        personTypeIndex: -1,
        // 行业字典
        merchantIndustryList: [],
        enterpriseIndustryList: [],
        industryIndex: -1,
        // 新型农业主体类型
        agricultureTypeList: [],
        agricultureTypeIndex: -1,
        scroller: {
			pageNum: 0,
			pageSize: 10,
			totalPage: 10,
			isNoneLoading: true,
			refreshColor: '#000000',
			refreshDiy: false,
			isloaded: false,
			list: []
        },
        
        show: false,
        actions: [],
        phone: "",
        // 转交
        depList: [],
        custList: [],
        showTransfer: false,
        transferForm: {
            areaName: "",
            handName: "",
            entName: "",
            areaId: "",
            merchantId: "",
            // 1-个人，2-商户，3-企业，4-新型农业主体
            merchantType: "",
            locationName: "",
            locationId: "",
            agrCustName: "",
            recipientDeptId: "",
            recipientId: ""
        },
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.initValidate()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        // this.getLatLng();
        this.getUserInfo();
        this.getDictionaryInfo("ecard_person_type");
        this.getDictionaryInfo("ecard_merchant_industry");
        this.getDictionaryInfo("ecard_enterprise_industry");
        this.getDictionaryInfo("ecard_agriculture_type");
        this.getSameLevelDepts();
        this.getSysConfigInfo1();
        this.getSysConfigInfo2();
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getDictionaryInfo("ecard_cust_type");
    },
    // 获取当前登录者的用户信息
    getUserInfo(e) {        
        request({
            url: "/user/getUserInfo",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {}
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    userInfo: res.data.userInfo
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /**
     * 获取字典
     */
    getDictionaryInfo(dictType) {
        request({
            url: "/commonApi/getDictionaryInfo",
            method: "GET",
            data: {
                dictType
            }
        }).then(res => {
            if(res.code == 0){
                if(dictType == 'ecard_cust_type'){
                    let subTab = [];
                    let typeList = [
                        {
                            dictLabel: "全部",
                            dictValue: ""
                        }
                    ];
                    subTab = res.data;
                    this.setData({
                        subActiveValue: subTab[0] ? subTab[0].dictValue: "",
                        subTab,
                        typeList: typeList.concat(subTab)
                    })
                    this.resetData();
                    this.getData();
                }
                if(dictType == 'ecard_person_type'){
                    let personTypeList = [
                        {
                            dictLabel: "全部",
                            dictValue: ""
                        }
                    ];
                    this.setData({
                        personTypeList: personTypeList.concat(res.data)
                    })
                }
                if(dictType == 'ecard_merchant_industry'){
                    let merchantIndustryList = [];
                    merchantIndustryList.push({
                        dictLabel: "全部",
                        dictValue: ""
                    })
                    this.setData({
                        merchantIndustryList: merchantIndustryList.concat(res.data)
                    })
                }
                if(dictType == 'ecard_enterprise_industry'){
                    let enterpriseIndustryList = [];
                    enterpriseIndustryList.push({
                        dictLabel: "全部",
                        dictValue: ""
                    })
                    this.setData({
                        enterpriseIndustryList: enterpriseIndustryList.concat(res.data)
                    })
                }
                if(dictType == 'ecard_agriculture_type'){
                    let agricultureTypeList = [];
                    agricultureTypeList.push({
                        dictLabel: "全部",
                        dictValue: ""
                    })
                    this.setData({
                        agricultureTypeList: agricultureTypeList.concat(res.data)
                    })
                }
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /**
     * 重置数据
     */
    resetData(){
		this.resetParams();
		this.resetScroller();
    },
    /**
     * 重置下拉
     */
    resetScroller(){
        let scroller = {
			pageNum: 0,
			pageSize: 10,
			totalPage: 10,
			isNoneLoading: true,
			refreshColor: '#000000',
			refreshDiy: false,
			isloaded: false,
			list: []
		}
		this.setData({
            scroller,
		})
    },
    /**
     * 验证params是否有值
     */
    validateParams(){
        let isFilter = false;
        let { params } = this.data;
        for(let key in params){
            console.log(params[key])
            if(params[key]){
                isFilter = true;
            }
        }
        this.setData({
            isFilter
		})
    },
    /**
     * 重置参数
     */
    resetParams(){
        let { params } = this.data;
        Object.keys(params).forEach((key) => (params[key] = ''))
		this.setData({
            params,
            industryIndex: -1,
            personTypeIndex: -1,
            typeIndex: -1,
            agricultureTypeIndex: -1,
            isFilter: false
		})
    },
    /** 分类点击事件*/
    subTabChange(e) {
        let { value } = e.currentTarget.dataset;
        this.setData({
            subActiveValue: value
        })
        this.resetData()
        this.getData();
    },
    /**
     * 显示筛选
     */
    showFilterPopup(){
        this.setData({
            showFilter: true
        })
    },
    /**
     * 关闭筛选
     */
    closeFilter(){
        this.setData({
            showFilter: false
        })
    },
    /**
     * 分类筛选
     */
    filterTypeChange(e){
        let { index } = e.currentTarget.dataset;
        this.setData({
            filterTypeIndex: index
        })
        this.resetScroller();
        this.getData();
    },
    /**
     * 确认筛选
     */
    confirmFilter(){
        this.setData({
            showFilter: false
        })
        this.validateParams();
        this.resetScroller();
        this.getData();
    },
    
    /**
     * 输入框输入时
     */
    input(e){
        let key = e.target.dataset.key;
        this.setData({
            ['params.'+key]: e.detail.value,
            // isFilter: !!e.detail.value,
        })
    },
    // 搜索
    searchConfirm(e){
        let key = e.currentTarget.dataset.key;
        this.setData({
            ['params.'+key]: e.detail.value,
            showFilter: false
        })
        this.validateParams();
        // 页面加载时执行一次加载
		this.resetScroller()
        this.getData();
    },
    // 搜索
    areaNameConfirm(e){
        console.log(e.detail.value)
        this.setData({
            'params.areaName': e.detail.value
        })
        this.validateParams();
        // 页面加载时执行一次加载
		this.resetScroller()
        this.getData();
    },
    searchBtn(e){
        this.resetScroller()
        this.getData();
    },
    bindTypeChange(e){
        this.setData({
            typeIndex: e.detail.value,
            industryIndex: -1,
            'params.industry': ""
        })
    },
    bindPersonTypeChange(e){
        this.setData({
            personTypeIndex: e.detail.value,
            'params.perType': this.data.personTypeList[e.detail.value].dictValue
        })
    },
    bindMerchantIndustryChange(e){
        this.setData({
            industryIndex: e.detail.value,
            'params.industry': this.data.merchantIndustryList[e.detail.value].dictValue
        })
    },
    bindEnterpriseIndustryChange(e){
        this.setData({
            industryIndex: e.detail.value,
            'params.industry': this.data.enterpriseIndustryList[e.detail.value].dictValue
        })
    },
    bindAgricultureTypeChange(e){
        this.setData({
            agricultureTypeIndex: e.detail.value,
            'params.agrType': this.data.agricultureTypeList[e.detail.value].dictValue
        })
    },
    /**
     * 获取数据
     */
    getData() {
        let { subActiveValue, filterTypeIndex } = this.data;
        switch(subActiveValue){
            case '1':
                // 个人列表
                if(filterTypeIndex == 0){
                    this.getMyPersonList();
                }
                // 网格列表
                if(filterTypeIndex == 1){
                    this.getPersonAreaInfoList();
                }
                break;
            case '2':
                this.getMyMerchantList();
                break;
            case '3':
                // 企业列表
                if(filterTypeIndex == 0){
                    this.getMyEnterpriseList();
                }
                // 楼宇列表
                if(filterTypeIndex == 1){
                    this.getLocationInfoList();
                }
                break;
            case '4':
                this.getMyAgricultureList()
                break;
            default: 
                break;
        }
    },

    /**
     * 获取我的个人
     */
    getMyPersonList() {
		let { scroller } = this.data;
        scroller.pageNum = scroller.pageNum + 1;
        if(scroller.pageNum>scroller.totalPage){
            return
        }
        request({
            url: `/personMarketing/getMyPersonList`,
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                ...this.data.params,
                pageNum: this.data.scroller.pageNum,
                pageSize: this.data.scroller.pageSize,
            }
        }).then(res => {
            if(res.code == 0){
                scroller.totalPage = Math.ceil(res.total/scroller.pageSize);
                if(scroller.pageNum>=scroller.totalPage){
                    scroller.isNoneLoading = false;
                }
                if(res.data.length>0){
                    res.data.map(item => {
                        item.creationTime = util.formatTime(new Date(item.creationTime));
                        item.lastVisitDate = item.lastVisitDate?util.formatTime(new Date(item.lastVisitDate)):"";
                        return item
                    })
                }
                if(scroller.pageNum == 1){
                    scroller.list = res.data;
                }else{
                    scroller.list = scroller.list.concat(res.data);
                }
                this.setData({
                    scroller
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /**
     * 获取我的个人网格
     */
    getPersonAreaInfoList() {
		let { scroller } = this.data;
        scroller.pageNum = scroller.pageNum + 1;
        if(scroller.pageNum>scroller.totalPage){
            return
        }
        request({
            url: `/personMarketing/getPersonAreaInfoList`,
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                ...this.data.params,
                type: 2,
                pageNum: this.data.scroller.pageNum,
                pageSize: this.data.scroller.pageSize,
            }
        }).then(res => {
            if(res.code == 0){
                scroller.totalPage = Math.ceil(res.total/scroller.pageSize);
                if(scroller.pageNum>=scroller.totalPage){
                    scroller.isNoneLoading = false;
                }
                if(res.data.length>0){
                    res.data.map(item => {
                        item.creationTime = util.formatTime(new Date(item.creationTime));
                        item.lastVisitDate = item.lastVisitDate?util.formatTime(new Date(item.lastVisitDate)):"";
                        return item
                    })
                }
                if(scroller.pageNum == 1){
                    scroller.list = res.data;
                }else{
                    scroller.list = scroller.list.concat(res.data);
                }
                this.setData({
                    scroller
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /**
     * 获取我的商户
     */
    getMyMerchantList() {
		let { scroller } = this.data;
        scroller.pageNum = scroller.pageNum + 1;
        if(scroller.pageNum>scroller.totalPage){
            return
        }
        request({
            url: `/merchantMarketing/getMyMerchantList`,
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                ...this.data.params,
                pageNum: this.data.scroller.pageNum,
                pageSize: this.data.scroller.pageSize,
            }
        }).then(res => {
            if(res.code == 0){
                scroller.totalPage = Math.ceil(res.total/scroller.pageSize);
                if(scroller.pageNum>=scroller.totalPage){
                    scroller.isNoneLoading = false;
                }
                if(res.data.length>0){
                    res.data.map(item => {
                        item.creationTime = util.formatTime(new Date(item.creationTime));
                        item.lastVisitDate = item.lastVisitDate?util.formatTime(new Date(item.lastVisitDate)):"";
                        return item
                    })
                }
                if(scroller.pageNum == 1){
                    scroller.list = res.data;
                }else{
                    scroller.list = scroller.list.concat(res.data);
                }
                this.setData({
                    scroller
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /**
     * 获取我的企业
     */
    getMyEnterpriseList() {
		let { scroller } = this.data;
        scroller.pageNum = scroller.pageNum + 1;
        if(scroller.pageNum>scroller.totalPage){
            return
        }
        request({
            url: `/enterpriseMarketing/getMyEnterpriseList`,
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                ...this.data.params,
                pageNum: this.data.scroller.pageNum,
                pageSize: this.data.scroller.pageSize,
            }
        }).then(res => {
            if(res.code == 0){
                console.log(res)
                scroller.totalPage = Math.ceil(res.total/scroller.pageSize);
                if(scroller.pageNum>=scroller.totalPage){
                    scroller.isNoneLoading = false;
                }
                if(res.data.length>0){
                    res.data.map(item => {
                        item.creationTime = util.formatTime(new Date(item.creationTime));
                        item.lastVisitDate = item.lastVisitDate?util.formatTime(new Date(item.lastVisitDate)):"";
                        return item
                    })
                }
                if(scroller.pageNum == 1){
                    scroller.list = res.data;
                }else{
                    scroller.list = scroller.list.concat(res.data);
                }
                this.setData({
                    scroller
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    
    /**
     * 获取我的楼宇
     */
    getLocationInfoList() {
		let { scroller } = this.data;
        scroller.pageNum = scroller.pageNum + 1;
        if(scroller.pageNum>scroller.totalPage){
            return
        }
        request({
            url: `/enterpriseMarketing/getLocationInfoList`,
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                ...this.data.params,
                type: 2,
                locationType: 1,
                pageNum: this.data.scroller.pageNum,
                pageSize: this.data.scroller.pageSize,
            }
        }).then(res => {
            if(res.code == 0){
                console.log(res)
                scroller.totalPage = Math.ceil(res.total/scroller.pageSize);
                if(scroller.pageNum>=scroller.totalPage){
                    scroller.isNoneLoading = false;
                }
                if(res.data.length>0){
                    res.data.map(item => {
                        item.creationTime = util.formatTime(new Date(item.creationTime));
                        item.lastVisitDate = item.lastVisitDate?util.formatTime(new Date(item.lastVisitDate)):"";
                        return item
                    })
                }
                if(scroller.pageNum == 1){
                    scroller.list = res.data;
                }else{
                    scroller.list = scroller.list.concat(res.data);
                }
                this.setData({
                    scroller
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /**
     * 获取我的新型农业主体
     */
    getMyAgricultureList() {
		let { scroller } = this.data;
        scroller.pageNum = scroller.pageNum + 1;
        if(scroller.pageNum>scroller.totalPage){
            return
        }
        request({
            url: `/agricultureMarketing/getMyAgricultureList`,
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                ...this.data.params,
                pageNum: this.data.scroller.pageNum,
                pageSize: this.data.scroller.pageSize,
            }
        }).then(res => {
            if(res.code == 0){
                console.log(res)
                scroller.totalPage = Math.ceil(res.total/scroller.pageSize);
                if(scroller.pageNum>=scroller.totalPage){
                    scroller.isNoneLoading = false;
                }
                if(res.data.length>0){
                    res.data.map(item => {
                        item.creationTime = util.formatTime(new Date(item.creationTime));
                        item.lastVisitDate = item.lastVisitDate?util.formatTime(new Date(item.lastVisitDate)):"";
                        return item
                    })
                }
                if(scroller.pageNum == 1){
                    scroller.list = res.data;
                }else{
                    scroller.list = scroller.list.concat(res.data);
                }
                this.setData({
                    scroller
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
	refresh() {
        this.resetData()
        this.getData()
    },
    
	loadMore() {
        console.log("loadMore")
		this.getData()
    },
    /**
     * 跳转网格个人
     */
    toAreadesc(e) {
        let { id, name, type } = e.currentTarget.dataset;
        wx.navigateTo({
          url: `/manager/pages/areadesc/index?id=${id}&name=${name}&merchantType=${type}`,
        })
    },
    /**
     * 跳转拜访记录
     */
    toVisit(e) {
        let { id, type } = e.currentTarget.dataset;
        wx.navigateTo({
          url: `/manager/pages/visit/index?merchantId=${id}&merchantType=${type}`,
        })
    },
    /**
     * 跳转楼宇企业
     */
    toBuildingdesc(e) {
        let { id, name, type } = e.currentTarget.dataset;
        wx.navigateTo({
          url: `/manager/pages/buildingdesc/index?id=${id}&name=${name}&merchantType=${type}`,
        })
    },

    /**
     * 跳转添加个人
     */
    toAddPerson(e) {
        wx.navigateTo({
          url: '/manager/pages/addpersonstep/index',
        })
    },

    /**
     * 跳转添加网格
     */
    toAddArea(e) {
        let { item, type } = e.currentTarget.dataset;
        if(item){
            wx.navigateTo({
                url: type?`/manager/pages/addarea/index?id=${item.id}&type=${type}`:`/manager/pages/addarea/index?id=${item.id}`,
            })
        }else{
            wx.navigateTo({
                url: '/manager/pages/addarea/index',
            })
        }
    },

    /**
     * 跳转添加商户
     */
    toAddMerchant(e) {
        wx.navigateTo({
          url: '/manager/pages/addmerchantstep/index',
        })
    },

    /**
     * 跳转添加企业
     */
    toAddEnterprise(e) {
        wx.navigateTo({
          url: '/manager/pages/addenterprisestep/index',
        })
    },

    /**
     * 跳转添加楼宇
     */
    toAddBuilding(e) {
        let { item, type } = e.currentTarget.dataset;
        if(item){
            wx.navigateTo({
                url: type?`/manager/pages/addbuilding/index?id=${item.id}&type=${type}`:`/manager/pages/addbuilding/index?id=${item.id}`,
            })
        }else{
            wx.navigateTo({
                url: '/manager/pages/addbuilding/index',
            })
        }
    },

    /**
     * 跳转添加新型农业主体
     */
    toAddNewagriculture(e) {
        wx.navigateTo({
            url: '/manager/pages/addnewagriculturestep/index',
        })
    },

    // 查看图片
    previewImage(e) {
        let { url } = e.currentTarget.dataset;
        if(url){
            wx.previewImage({
                current: url, // 当前显示图片的http链接
                urls: [url] // 需要预览的图片http链接列表
            })
        }
    },

    /**
     * 拨打电话
     */
    makePhoneCall(e) {
        console.log(e)
        let { phone } = e.currentTarget.dataset;
        if(!phone){
            wx.showToast({
                title: "暂无联系方式",
                icon: 'none',
                duration: 2000
            })
            return
        }
        getSystemInfo({
            android: () => {
                let actions = [
                    { name: phone, color: '#999', disabled: true },
                    { name: '呼叫', color: '#000' },
                ]
                this.setData({
                    show: true,
                    phone: phone,
                    actions
                })
            },
            ios: () => {
                wx.makePhoneCall({
                    phoneNumber: phone
                })
            }
        })
    },
    onSelect(e) {
        console.log(e)
        this.setData({
            show: false
        })
        wx.makePhoneCall({
            phoneNumber: this.data.phone
        })
    },
    onCancel() {
        this.setData({ show: false });
    },

    /** 导航 */
    navigateTo(e) {
        // console.log(e.currentTarget.dataset)
        let { coordinate, name } = e.currentTarget.dataset;
        if(!coordinate){
            wx.showToast({
                title: "暂无位置信息",
                icon: 'none',
                duration: 2000
            })
            return
        }
        let arr = coordinate.split(",")
        wx.openLocation({
            name,
            latitude: Number(arr[0]),
            longitude: Number(arr[1])
        })
    },

    /**
     * 获取同级部门列表
     */
    getSameLevelDepts() {
        request({
            url: `/dept/getSameLevelDepts`,
            method: "GET"
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    depList: res.data
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    /**
     * 根据机构ID获取客户经理
     */
    getUserListByDeptId(deptId) {
        request({
            url: `/user/getUserListByDeptId`,
            method: "GET",
            reqRSAENCRY: false, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                deptId
            }
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    custList: res.data
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    /**
     * 获取删除权限
     */
    getSysConfigInfo1() {
        request({
            url: "/commonApi/getSysConfigInfo",
            method: "GET",
            data: {
                configKey: "ecard_merchant_isdelete"
            }
        }).then(res => {
            console.log(res)
            if(res.code == 0){
                this.setData({
                    isDelete: Number(res.data.configValue),
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /**
     * 获取删除流程权限
     */
    getSysConfigInfo2() {
        request({
            url: "/commonApi/getSysConfigInfo",
            method: "GET",
            data: {
                configKey: "ecard_del_flow_sts"
            }
        }).then(res => {
            console.log(res)
            if(res.code == 0){
                // 1启用，2停用
                this.setData({
                    isDeleteProcess: Number(res.data.configValue),
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    /**
     * 选择框变化时
     */
    pickerChagne(e){
        let { key, value } = e.detail;
        if(key == "recipientDeptId"){
            if(value == this.data.transferForm.recipientDeptId){
               return 
            }
            this.getUserListByDeptId(value)
            this.setData({
                ['transferForm.'+key]: value,
                "transferForm.recipientId": ""
            })
        }else{
            this.setData({
                ['transferForm.'+key]: value
            })
        }
        
    },
    /** 转交弹窗 */
    transferMerchant(e){
        let { item } = e.currentTarget.dataset;
        let { subActiveValue, filterTypeIndex, transferForm } = this.data;
        Object.keys(transferForm).forEach(key => transferForm[key] = "")
        // 个人
        if(subActiveValue == 1 && filterTypeIndex == 0){
            transferForm.perCustName = item.perCustName;
            transferForm.merchantId = item.id;
            transferForm.merchantType = 1;
            this.setData({
                showTransfer: true,
                transferForm
            })
        }
        // 个人网格
        if(subActiveValue == 1 && filterTypeIndex == 1){
            transferForm.areaName = item.areaName;
            transferForm.areaId = item.id;
            transferForm.merchantType = 1;
            this.setData({
                showTransfer: true,
                transferForm
            })
        }
        // 商户
        if(subActiveValue == 2){
            transferForm.handName = item.handName;
            transferForm.merchantId = item.id;
            transferForm.merchantType = 2;
            this.setData({
                showTransfer: true,
                transferForm
            })
        }
        // 企业
        if(subActiveValue == 3 && filterTypeIndex == 0){
            transferForm.entName = item.entName;
            transferForm.merchantId = item.id;
            transferForm.merchantType = 3;
            this.setData({
                showTransfer: true,
                transferForm
            })
        }
        // 楼宇
        if(subActiveValue == 3 && filterTypeIndex == 1){
            transferForm.locationName = item.locationName;
            transferForm.locationId = item.id;
            transferForm.merchantType = 3;
            this.setData({
                showTransfer: true,
                transferForm
            })
        }
        // 新型农业主体
        if(subActiveValue == 4){
            transferForm.agrCustName = item.agrCustName;
            transferForm.merchantId = item.id;
            transferForm.merchantType = 4;
            this.setData({
                showTransfer: true,
                transferForm
            })
        }
        
    },
    /** 取消转交 */
	closeTransfer(e){
		this.setData({
			showTransfer: false
		})
	},
	/** 确认转交 */
	confirmTransfer(e){
        let { subActiveValue, filterTypeIndex, transferForm } = this.data;
        // 传入表单数据，调用验证方法
        if (!this.WxValidate.checkForm(transferForm)) {
            const error = this.WxValidate.errorList[0]
            this.showModal(error)
            return false
        }
        // 网格
        if(subActiveValue == 1 && filterTypeIndex == 1){
            request({
                url: `/personMarketing/addAreaTransferInfo`,
                reqRSAENCRY: true, // 参数是否加密
                resRSADECRY: false, // 返回值是否解密
                data: transferForm
            }).then(res => {
                if(res.code == 0){
                    wx.showToast({
                        title: '转交成功',
                        icon: 'none',
                        mask: true,
                        duration: 2000
                    });
                    this.setData({
                        showTransfer: false
                    })
                    setTimeout(()=>{
                        this.resetScroller()
                        this.getData();
                    },2000)
                }
            }).catch(error => {
                // console.log(error)
            })
        }
        // 个人、商户、企业、新型农业主体
        if((subActiveValue == 1 && filterTypeIndex == 0) || subActiveValue == 2 || (subActiveValue == 3 && filterTypeIndex == 0) || subActiveValue == 4){
            request({
                url: `/merchantMarketing/addMerchantTransferInfo`,
                reqRSAENCRY: true, // 参数是否加密
                resRSADECRY: false, // 返回值是否解密
                data: transferForm
            }).then(res => {
                if(res.code == 0){
                    wx.showToast({
                        title: '转交成功',
                        icon: 'none',
                        mask: true,
                        duration: 2000
                    });
                    this.setData({
                        showTransfer: false
                    })
                    setTimeout(()=>{
                        this.resetScroller()
                        this.getData();
                    },2000)
                }
            }).catch(error => {
                // console.log(error)
            })
        }
        // 楼宇
        if(subActiveValue == 3 && filterTypeIndex == 1){
            request({
                url: `/enterpriseMarketing/addLocationTransferInfo`,
                reqRSAENCRY: true, // 参数是否加密
                resRSADECRY: false, // 返回值是否解密
                data: transferForm
            }).then(res => {
                if(res.code == 0){
                    wx.showToast({
                        title: '转交成功',
                        icon: 'none',
                        mask: true,
                        duration: 2000
                    });
                    this.setData({
                        showTransfer: false
                    })
                    setTimeout(()=>{
                        this.resetScroller()
                        this.getData();
                    },2000)
                }
            }).catch(error => {
                // console.log(error)
            })
        }
    },
    // 表单验证
    initValidate() {
        // 验证字段的规则
        const rules = {
            recipientDeptId: {
                required: true
            },
            recipientId: {
                required: true
            }
        }
        // 验证字段的提示信息，若不传则调用默认的信息
        const messages = {
            recipientDeptId: {
                required: '请选择转交机构',
            },
            recipientId: {
                required: '请选择转交客户经理'
            }
        }

        // 创建实例对象
        this.WxValidate = new WxValidate(rules, messages)
    },
    showModal(error) {
        wx.showToast({
            title: error.msg,
            icon: 'none',
            duration: 2000
        })
    },
    /** 删除个人 */
    deletePreson(e){
        let { item } = e.currentTarget.dataset;
        let that = this;
        // 当参数值为0时，允许员工删除；当参数值为1时，则删除时提醒“请联系管理员删除该企业信息”。
        if(this.data.isDelete == 0){
            wx.showModal({
                title: '提示',
                content: `是否删除该个人？`,
                success (res) {
                    if (res.confirm) {
                        if(that.data.isDeleteProcess == 1){
                            request({
                                url: `/businessFlow/addApprovalProcess`,
                                // method: "GET",
                                reqRSAENCRY: true, // 参数是否加密
                                resRSADECRY: true, // 返回值是否解密
                                data: {
                                    flowType: "card_del",
                                    busiId: item.id,
                                    // busiType  1-商户信息，2-企业信息，3-企业楼宇（厂区），4-个人信息，5-个人网格，6.-农业主体信息
                                    busiType: 4,
                                    keyInfo: {
                                        cname: item.perCustName,
                                        busiId: item.id,
                                        busiType: 4,
                                    }
                                }
                            }).then(res => {
                                if(res.code == 0){
                                    wx.navigateTo({
                                        url: '/manager/pages/todotask/index',
                                    })
                                }
                            }).catch(error => {
                                // console.log(error)
                            })
                        }else{
                            request({
                                url: `/personMarketing/changePersonStatus`,
                                // method: "GET",
                                reqRSAENCRY: true, // 参数是否加密
                                resRSADECRY: false, // 返回值是否解密
                                data: {
                                    id: item.id
                                }
                            }).then(res => {
                                if(res.code == 0){
                                    if(res.code == 0){
                                        wx.showToast({
                                            title: '删除成功',
                                            icon: 'none',
                                            mask: true,
                                            duration: 2000
                                        });
                                        setTimeout(()=>{
                                            that.resetScroller()
                                            that.getData();
                                        },2000)
                                    }
                                }
                            }).catch(error => {
                                // console.log(error)
                            })
                        }
                        // console.log('用户点击确定')
                    } else if (res.cancel) {
                        // console.log('用户点击取消')
                    }
                }
            })
        }else{
            wx.showToast({
                title: '请联系管理员删除该个人信息',
                icon: 'none',
                duration: 2000
            });
        }
    },
    /** 删除网格 */
    deleteArea(e){
        let { item } = e.currentTarget.dataset;
        let that = this;
        // 当参数值为0时，允许员工删除；当参数值为1时，则删除时提醒“请联系管理员删除该企业信息”。
        if(this.data.isDelete == 0){
            wx.showModal({
                title: '提示',
                content: `是否删除该网格？`,
                success (res) {
                    if (res.confirm) {
                        if(that.data.isDeleteProcess == 1){
                            if(item.isDelete == 0){
                                wx.showToast({
                                    title: '该网格下存在个人信息，无法删除！',
                                    icon: 'none',
                                    duration: 2000
                                });
                            }else{
                                request({
                                    url: `/businessFlow/addApprovalProcess`,
                                    // method: "GET",
                                    reqRSAENCRY: true, // 参数是否加密
                                    resRSADECRY: true, // 返回值是否解密
                                    data: {
                                        flowType: "card_del",
                                        busiId: item.id,
                                        // busiType  1-商户信息，2-企业信息，3-企业楼宇（厂区），4-个人信息，5-个人网格，6.-农业主体信息
                                        busiType: 5,
                                        keyInfo: {
                                            cname: item.areaName,
                                            busiId: item.id,
                                            busiType: 5,
                                        }
                                    }
                                }).then(res => {
                                    if(res.code == 0){
                                        wx.navigateTo({
                                            url: '/manager/pages/todotask/index',
                                        })
                                    }
                                }).catch(error => {
                                    // console.log(error)
                                })
                            }
                        }else{
                            request({
                                url: `/personMarketing/changePersonAreaStatus`,
                                // method: "GET",
                                reqRSAENCRY: true, // 参数是否加密
                                resRSADECRY: false, // 返回值是否解密
                                data: {
                                    id: item.id
                                }
                            }).then(res => {
                                if(res.code == 0){
                                    if(res.code == 0){
                                        wx.showToast({
                                            title: '删除成功',
                                            icon: 'none',
                                            mask: true,
                                            duration: 2000
                                        });
                                        setTimeout(()=>{
                                            that.resetScroller()
                                            that.getData();
                                        },2000)
                                    }
                                }
                            }).catch(error => {
                                // console.log(error)
                            })
                        }
                        // console.log('用户点击确定')
                    } else if (res.cancel) {
                        // console.log('用户点击取消')
                    }
                }
            })
        }else{
            wx.showToast({
                title: '请联系管理员删除该网格信息',
                icon: 'none',
                duration: 2000
            });
        }
    },
    
    /** 删除商户 */
    deleteMerchant(e){
        let { item } = e.currentTarget.dataset;
        let that = this;
        // 当参数值为0时，允许员工删除；当参数值为1时，则删除时提醒“请联系管理员删除该商户信息”。
        if(this.data.isDelete == 0){
            wx.showModal({
                title: '提示',
                content: `是否删除该商户？`,
                success (res) {
                    if (res.confirm) {
                        if(that.data.isDeleteProcess == 1){
                            request({
                                url: `/businessFlow/addApprovalProcess`,
                                // method: "GET",
                                reqRSAENCRY: true, // 参数是否加密
                                resRSADECRY: true, // 返回值是否解密
                                data: {
                                    flowType: "card_del",
                                    busiId: item.id,
                                    // busiType  1-商户信息，2-企业信息，3-企业楼宇（厂区），4-个人信息，5-个人网格，6.-农业主体信息
                                    busiType: 1,
                                    keyInfo: {
                                        cname: item.handName,
                                        busiId: item.id,
                                        busiType: 1,
                                    }
                                }
                            }).then(res => {
                                if(res.code == 0){
                                    wx.navigateTo({
                                        url: '/manager/pages/todotask/index',
                                    })
                                }
                            }).catch(error => {
                                // console.log(error)
                            })
                        }else{
                            request({
                                url: `/merchantMarketing/changeMerchantStatus`,
                                // method: "GET",
                                reqRSAENCRY: true, // 参数是否加密
                                resRSADECRY: false, // 返回值是否解密
                                data: {
                                    id: item.id
                                }
                            }).then(res => {
                                if(res.code == 0){
                                    if(res.code == 0){
                                        wx.showToast({
                                            title: '删除成功',
                                            icon: 'none',
                                            mask: true,
                                            duration: 2000
                                        });
                                        setTimeout(()=>{
                                            that.resetScroller()
                                            that.getData();
                                        },2000)
                                    }
                                }
                            }).catch(error => {
                                // console.log(error)
                            })
                        }
                        // console.log('用户点击确定')
                    } else if (res.cancel) {
                        // console.log('用户点击取消')
                    }
                }
            })
        }else{
            wx.showToast({
                title: '请联系管理员删除该商户信息',
                icon: 'none',
                duration: 2000
            });
        }
    },
    
    /** 删除企业 */
    deleteEnterprise(e){
        let { item } = e.currentTarget.dataset;
        let that = this;
        // 当参数值为0时，允许员工删除；当参数值为1时，则删除时提醒“请联系管理员删除该企业信息”。
        if(this.data.isDelete == 0){
            wx.showModal({
                title: '提示',
                content: `是否删除该企业？`,
                success (res) {
                    if (res.confirm) {
                        if(that.data.isDeleteProcess == 1){
                            request({
                                url: `/businessFlow/addApprovalProcess`,
                                // method: "GET",
                                reqRSAENCRY: true, // 参数是否加密
                                resRSADECRY: true, // 返回值是否解密
                                data: {
                                    flowType: "card_del",
                                    busiId: item.id,
                                    // busiType  1-商户信息，2-企业信息，3-企业楼宇（厂区），4-个人信息，5-个人网格，6.-农业主体信息
                                    busiType: 2,
                                    keyInfo: {
                                        cname: item.entName,
                                        busiId: item.id,
                                        busiType: 2,
                                    }
                                }
                            }).then(res => {
                                if(res.code == 0){
                                    wx.navigateTo({
                                        url: '/manager/pages/todotask/index',
                                    })
                                }
                            }).catch(error => {
                                // console.log(error)
                            })
                        }else{
                            request({
                                url: `/enterpriseMarketing/changeEnterpriseStatus`,
                                // method: "GET",
                                reqRSAENCRY: true, // 参数是否加密
                                resRSADECRY: false, // 返回值是否解密
                                data: {
                                    id: item.id
                                }
                            }).then(res => {
                                if(res.code == 0){
                                    if(res.code == 0){
                                        wx.showToast({
                                            title: '删除成功',
                                            icon: 'none',
                                            mask: true,
                                            duration: 2000
                                        });
                                        setTimeout(()=>{
                                            that.resetScroller()
                                            that.getData();
                                        },2000)
                                    }
                                }
                            }).catch(error => {
                                // console.log(error)
                            })
                        }
                        // console.log('用户点击确定')
                    } else if (res.cancel) {
                        // console.log('用户点击取消')
                    }
                }
            })
        }else{
            wx.showToast({
                title: '请联系管理员删除该企业信息',
                icon: 'none',
                duration: 2000
            });
        }
    },

    /** 删除楼宇 */
    deleteBuilding(e){
        let { item } = e.currentTarget.dataset;
        let that = this;
        // 当参数值为0时，允许员工删除；当参数值为1时，则删除时提醒“请联系管理员删除该企业信息”。
        if(this.data.isDelete == 0){
            wx.showModal({
                title: '提示',
                content: `是否删除该楼宇？`,
                success (res) {
                    if (res.confirm) {
                        if(that.data.isDeleteProcess == 1){
                            if(item.isDelete == 0){
                                wx.showToast({
                                    title: '该楼宇下存在企业信息，无法删除！',
                                    icon: 'none',
                                    duration: 2000
                                });
                            }else{
                                request({
                                    url: `/businessFlow/addApprovalProcess`,
                                    // method: "GET",
                                    reqRSAENCRY: true, // 参数是否加密
                                    resRSADECRY: true, // 返回值是否解密
                                    data: {
                                        flowType: "card_del",
                                        busiId: item.id,
                                        // busiType  1-商户信息，2-企业信息，3-企业楼宇（厂区），4-个人信息，5-个人网格，6.-农业主体信息
                                        busiType: 3,
                                        keyInfo: {
                                            cname: item.locationName,
                                            busiId: item.id,
                                            busiType: 3,
                                        }
                                    }
                                }).then(res => {
                                    if(res.code == 0){
                                        wx.navigateTo({
                                            url: '/manager/pages/todotask/index',
                                        })
                                    }
                                }).catch(error => {
                                    // console.log(error)
                                })
                            }
                        }else{
                            request({
                                url: `/enterpriseMarketing/changeLocationStatus`,
                                // method: "GET",
                                reqRSAENCRY: true, // 参数是否加密
                                resRSADECRY: false, // 返回值是否解密
                                data: {
                                    id: item.id
                                }
                            }).then(res => {
                                if(res.code == 0){
                                    if(res.code == 0){
                                        wx.showToast({
                                            title: '删除成功',
                                            icon: 'none',
                                            mask: true,
                                            duration: 2000
                                        });
                                        setTimeout(()=>{
                                            that.resetScroller()
                                            that.getData();
                                        },2000)
                                    }
                                }
                            }).catch(error => {
                                // console.log(error)
                            })
                        }
                        // console.log('用户点击确定')
                    } else if (res.cancel) {
                        // console.log('用户点击取消')
                    }
                }
            })
        }else{
            wx.showToast({
                title: '请联系管理员删除该楼宇信息',
                icon: 'none',
                duration: 2000
            });
        }
    },

    /** 删除新型农业主体 */
    deleteAgriculture(e){
        let { item } = e.currentTarget.dataset;
        let that = this;
        // 当参数值为0时，允许员工删除；当参数值为1时，则删除时提醒“请联系管理员删除该企业信息”。
        if(this.data.isDelete == 0){
            wx.showModal({
                title: '提示',
                content: `是否删除该新型农业主体？`,
                success (res) {
                    if (res.confirm) {
                        if(that.data.isDeleteProcess == 1){
                            request({
                                url: `/businessFlow/addApprovalProcess`,
                                // method: "GET",
                                reqRSAENCRY: true, // 参数是否加密
                                resRSADECRY: true, // 返回值是否解密
                                data: {
                                    flowType: "card_del",
                                    busiId: item.id,
                                    // busiType  1-商户信息，2-企业信息，3-企业楼宇（厂区），4-个人信息，5-个人网格，6.-农业主体信息
                                    busiType: 6,
                                    keyInfo: {
                                        cname: item.agrCustName,
                                        busiId: item.id,
                                        busiType: 6,
                                    }
                                }
                            }).then(res => {
                                if(res.code == 0){
                                    wx.navigateTo({
                                        url: '/manager/pages/todotask/index',
                                    })
                                }
                            }).catch(error => {
                                // console.log(error)
                            })
                        }else{
                            request({
                                url: `/agricultureMarketing/changeAgricultureStatus`,
                                // method: "GET",
                                reqRSAENCRY: true, // 参数是否加密
                                resRSADECRY: false, // 返回值是否解密
                                data: {
                                    id: item.id
                                }
                            }).then(res => {
                                if(res.code == 0){
                                    if(res.code == 0){
                                        wx.showToast({
                                            title: '删除成功',
                                            icon: 'none',
                                            mask: true,
                                            duration: 2000
                                        });
                                        setTimeout(()=>{
                                            that.resetScroller()
                                            that.getData();
                                        },2000)
                                    }
                                }
                            }).catch(error => {
                                // console.log(error)
                            })
                        }
                        // console.log('用户点击确定')
                    } else if (res.cancel) {
                        // console.log('用户点击取消')
                    }
                }
            })
        }else{
            wx.showToast({
                title: '请联系管理员删除该新型农业主体信息',
                icon: 'none',
                duration: 2000
            });
        }
    },

    /**
     * 跳转额度测算
     */
    toCalculation(e){
        let { item } = e.currentTarget.dataset;
        console.log(item)
        wx.navigateTo({
          url: `/manager/pages/calculation/index?merchantId=${item.id}`,
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },
})